package com.xcc.wchatrobot;

import android.accessibilityservice.AccessibilityService;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.ArrayList;
import java.util.List;

public class SendMsgTools extends Handler implements UserMsg.OnMsgListener {
    private static final int Finish = -1;//执行结束
    private static final int FindUserName = 0;//查找用户名称
    private static final int SetEditText = 1;//循环发送监听
    private static final int SendMsg = 2;//循环发送监听
    private static final int ClickBack = 3;//点击返回按钮
    private MyAccessibilityService service;
    private AccessibilityNodeInfo info;
    private UserMsg userMsg;
    private List<String> msgList = new ArrayList<>();//信息缓存

    public SendMsgTools setService(MyAccessibilityService service) {
        this.service = service;
        return this;
    }

    public void handleMessage(Message msg) {
        if (service == null) {//直接结束
            info = null;
            return;
        }
        IdControl idControl = IdControl.getInstance();
        switch (msg.what) {
            case FindUserName://1.查找用户名称
                String text = service.getText(info, idControl.userChatUserName());
                if (TextUtils.isEmpty(text)) {// 点击返回按钮
                    sendEmptyMessageDelayed(ClickBack, 100);
                } else {
                    userMsg = MsgManage.getInstance().getUserMsg(text);
                    if (userMsg.total <= 0) {//没有信息
                        // 点击返回按钮
                        sendEmptyMessageDelayed(ClickBack, 100);
                    } else {
                        userMsg.setOnMsgListener(this);
                        sendEmptyMessage(SetEditText);
                    }
                }
                break;
            case SetEditText://设置输入框信息
                if (msgList.size() > 0) {//发送一条信息
                    String s = msgList.remove(0);
                    service.setEditText(info, idControl.userChatEditContent(), s);
                    sendEmptyMessageDelayed(SendMsg, 100);
                } else {
                    if (userMsg.total <= 0) {//没有信息
                        sendEmptyMessageDelayed(ClickBack, 100);
                    } else {
                        sendEmptyMessageDelayed(SetEditText, 100);
                    }
                }
                break;
            case SendMsg://点击发送按钮
                service.clickView(info, idControl.userChatSend());
                sendEmptyMessage(SetEditText);
                break;
            case ClickBack://点击返回按钮
                if (userMsg != null) {
                    userMsg.total = 0;
                    userMsg.setOnMsgListener(null);
                    userMsg = null;
                }
                service.clickView(info, idControl.userChatBack());
                sendEmptyMessageDelayed(Finish, 100);
                //service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                //service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
                info = null;
                break;
            case Finish:
                service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                break;
        }
    }

    public void start(AccessibilityNodeInfo info) {
        if (this.info == null) {
            this.info = info;
            sendEmptyMessage(FindUserName);
        }
    }


    public void OnMsg(List<String> msgList) {
        this.msgList.addAll(msgList);
    }
}
